package com.example.fengshoutong.utils;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;

import com.example.fengshoutong.R;
import com.example.fengshoutong.ui.ArticleFragment;
import com.example.fengshoutong.ui.CarFragment;
import com.example.fengshoutong.ui.ChatFragment;
import com.example.fengshoutong.ui.GoodFragment;
import com.example.fengshoutong.ui.HomeFragment;
import com.example.fengshoutong.ui.LoginFragment;
import com.example.fengshoutong.ui.MessageFragment;
import com.example.fengshoutong.ui.MineFragment;
import com.example.fengshoutong.ui.PasswordFragment;
import com.example.fengshoutong.ui.PublishArticleFragment;
import com.example.fengshoutong.ui.PublishFragment;
import com.example.fengshoutong.ui.PublishGoodFragment;
import com.example.fengshoutong.ui.SchoolFragment;
import com.example.fengshoutong.ui.SellFragment;
import com.example.fengshoutong.ui.SettingsFragment;

/**
 * Fragment工具类，用于切换不同的Fragment
 */
public class FragmentUtils {
    private static FragmentUtils instance;
    private FragmentManager fragmentManager;

    /**
     * 单例模式
     *
     * @return
     */
    public static FragmentUtils getInstance() {
        if (instance == null) {
            instance = new FragmentUtils();
        }
        return instance;
    }

    public void init(FragmentManager fragmentManager) {
        this.fragmentManager = fragmentManager;
    }

    /**
     * 根据menu中的id切换Fragment
     *
     * @param id
     */
    public void switchFragment(int id) {
        Fragment selectedFragment = null;
        if (id == R.id.nav_home) {
            selectedFragment = new HomeFragment();
        }
        else if (id == R.id.nav_school) {
           selectedFragment = new SchoolFragment();
       }
        else if (id == R.id.nav_publish) {
            selectedFragment = new PublishFragment();
        }
        else if (id == R.id.nav_message) {
           selectedFragment = new MessageFragment();
        }
        else if (id == R.id.nav_mine) {
            selectedFragment = new MineFragment();
        }
        else if (id == R.id.nav_sell) {
            selectedFragment = new SellFragment();
        }
        else if (id == R.id.nav_setting) {
            selectedFragment = new SettingsFragment();
        }
        else if (id == R.id.nav_chat) {
            selectedFragment = new ChatFragment();
        }
        else if (id == R.id.nav_login) {
            selectedFragment = new LoginFragment();
        }
        else if (id == R.id.nav_password) {
            selectedFragment = new PasswordFragment();
        }
        else if (id == R.id.nav_publish_good) {
            selectedFragment = new PublishGoodFragment();
        }
        else if (id == R.id.nav_publish_article) {
            selectedFragment = new PublishArticleFragment();
        }
        else if (id == R.id.nav_article) {
            selectedFragment = new ArticleFragment();
        }
        else if (id == R.id.nav_good) {
            selectedFragment = new GoodFragment();
        }
        else if (id == R.id.nav_car) {
            selectedFragment = new CarFragment();
        }
        if (selectedFragment != null) {
            fragmentManager.beginTransaction().replace(R.id.fragment_container, selectedFragment).commit();
        }
    }
}
